import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MoveingSeceneBg')
export class MoveingSeceneBg extends Component {
    @property(Node)
    public bg1: Node;
    @property(Node)
    public bg2: Node;

    private _bgSpeed = 10;
    private _bgMoveingRange = 130;
    start() {
        this._initBg();
    }

    update(deltaTime: number) {
        this._moveBg(deltaTime);
    }

    private _initBg() {
        this.bg1.setPosition(0, 0, 0);
        this.bg2.setPosition(0, 0, this._bgMoveingRange);
    }

    private _moveBg(deltaTime: number) {
        this.bg1.setPosition(0, 0, this.bg1.position.z + this._bgSpeed * deltaTime);
        this.bg2.setPosition(0, 0, this.bg2.position.z + this._bgSpeed * deltaTime);

        if (this.bg1.position.z > this._bgMoveingRange) {
            this.bg1.setPosition(0, 0, this.bg2.position.z - this._bgMoveingRange);
        } else if (this.bg2.position.z > this._bgMoveingRange) {
            this.bg2.setPosition(0, 0, this.bg1.position.z - this._bgMoveingRange);
        }
    }
}


